import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import pandas as pd
import datetime
import re
import time

class Pachong:
    url = ''
    selector = ''
    parserText = []
    mouseXpath=''
    keyXpath=''

    

    def __init__(self,url,selector='',parserText=[],mouseXpath='',keyXpath=''):
        self.url = url
        self.selector = selector
        self.parserText = parserText
        self.mouseXpath = mouseXpath
        self.keyXpath = keyXpath




    def get_html4requests(self):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
        response = requests.get(self.url, headers=headers)
        response.encoding = 'GB2312'
        return response.text

    '''
    url ：网址
    fag=False  # 是否使用无头浏览器
    '''
    def get_html4selenium(self,fag=False):
        
        s = Service('C:\python312\Scripts\chromedriver.exe')
        options = webdriver.ChromeOptions()
        options.add_argument('--headless')
        options.add_argument('--disable-gpu')
        if fag == True:
            driver = webdriver.Chrome(service=s)
        else:
            driver = webdriver.Chrome(service=s,options=options)
            
        driver.get(self.url)
        driver.maximize_window
        time.sleep(3)
        html = driver.page_source
        driver.quit()
        return html

    def parse_html(self,html):
        soup = BeautifulSoup(html, 'html.parser')
        # 解析网页内容

        result = soup.select(self.selector)
        resultList = []
        for i in result:
            resultList.append(i.get_text())
        # 选择器
        return list(resultList)

    def parse_html4re(self,html):
        title = re.findall(self.parserText[0], html, re.S)
        target = re.findall(self.parserText[1], html, re.S)
        return list(zip(title, target))


    def save_html(self,contectList,title='',columnNames=[]):
        nowtime = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        df=pd.DataFrame(contectList,columns=columnNames)
        df.to_csv(f'{title}{nowtime}.csv',index=False)

    def clearContent(self,content):
        for i in range(len(content)):
            content[i] = content[i].replace('"','').replace('“','').strip()

            
        return content

if __name__ == '__main__':
    url = 'https://www.baidu.com'
    selector = 'span'
    x = Pachong(url,selector)
    html=x.get_html4requests()
    print(html)
